package ch.zbw.pra.sysventory.application.controller;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.zbw.pra.sysventory.business.businessObject.scan.ScanBusinessObject;
import ch.zbw.pra.sysventory.model.Scanversion;

/**
 * Controller für den Scan
 * 
 * @author Ralph Koster
 */

public class ScanController implements Serializable {

	private static final long serialVersionUID = 916227325456820633L;
	private static final Logger LOG = LoggerFactory.getLogger(ScanController.class);
	
	private ScanBusinessObject scanBusinessObject;
	private String subnetMask;
	private boolean scanStarted = false;
	
	public ScanController() {
		this.scanBusinessObject = new ScanBusinessObject();
	}

	public Scanversion doScan() {
		Scanversion scanversion = new Scanversion();
		if(!scanStarted) {
			scanBusinessObject.setSubnetMask(subnetMask);
			try {
				scanStarted = true;
				scanversion = scanBusinessObject.call();
			} catch (Exception e) {
				LOG.debug("Failed to load new Scanversion");
				LOG.error(e.toString());
			}
			scanStarted = false;
		}
		return scanversion;
	}
	
	public void cancelScan() {
		scanBusinessObject.interruptAllThreads();
		scanStarted = false;
	}

	public void setSubnetMask(String subnetMask) {
		this.subnetMask = subnetMask;
	}
}
